import React from 'react';
import PixelRatio from 'PixelRatio';
import Platform from 'Platform';

var instance = null;
export default class SizeUtils {
  constructor() {
    if(!instance) {
      instance = this;

      this._designWidth = 750;
      this._designHeight = 1373;

      this._windowWidth = 0;
      this._windowHeight = 0;

      this._scaleWidth = 1;
      this._scaleHeight = 1;
      this.orientation = 0; // 0为竖屏 1为横屏
    }
    return instance;
  }

  setWindowSize(width, height) {
    this._windowWidth = width;
    this._windowHeight = height;

    this._scaleWidth = this._windowWidth * PixelRatio.get() / this._designWidth;
    // this._scaleWidth = 1;
    this._scaleHeight = this._windowHeight * PixelRatio.get() / this._designHeight;
    // this._scaleHeight = 1;
    console.log("setWindowSize", this._windowWidth, this._windowHeight, this._scaleWidth, this._scaleHeight);
  }

  getDesignSize() {
    return {width:this._designWidth, height:this._designHeight};
  }

  // 切换成横屏
  changeLandscape(){
    if(this.orientation == 0){
      this.orientation  =1;
      this.change();
       console.log("changeLandscape", this._windowWidth, this._windowHeight, this._scaleWidth, this._scaleHeight);
    }
  }

  //锁定为竖屏
  lockToPortrait(){
    if(this.orientation == 1){
      this.orientation = 0;
      this.change();
      console.log("lockToPortrait", this._windowWidth, this._windowHeight, this._scaleWidth, this._scaleHeight);
    }
  }

  change(){
      this._windowHeight = this._windowWidth + this._windowHeight;
      this._windowWidth = this._windowHeight - this._windowWidth;
      this._windowHeight = this._windowHeight - this._windowWidth;

      this._scaleHeight = this._scaleWidth+ this._scaleHeight;
      this._scaleWidth = this._scaleHeight - this._scaleWidth;
      this._scaleHeight = this._scaleHeight - this._scaleWidth;
  }

  getWindowSize() {
    // console.log("getWindowSize", {width:this._windowWidth, height:this._windowHeight});
    return {width:this._windowWidth, height:this._windowHeight -  (Platform.OS === "ios"?0:this.translateDesignHeightToCode(64))}
  }

  getWidthScale() {
    return this._scaleWidth;
  }

  //把设计图上的尺寸变为代码用的
  translateDesignWidthToCode(width) {
    // console.log("translateDesignWidthToCode", width, width * this._scaleWidth / PixelRatio.get());
    return Math.floor(width * this._scaleWidth / PixelRatio.get());
  }

  //把设计图上的尺寸变为代码用的
  translateDesignHeightToCode(height) {
    // console.log("translateDesignWidthToCode", width, width * this._scaleWidth / PixelRatio.get());
    return Math.floor(height * this._scaleHeight / PixelRatio.get());
  }

  translateDesignNumToCode(num) {
    return Math.floor(num / PixelRatio.get());
  }

  translateFontSizeToCode(size) {
    return Math.floor(size / PixelRatio.getFontScale() * this._scaleWidth);
  }
}

new SizeUtils();
module.exports = instance;
